const http = require("../../module/http")
const app =  getApp();

Page({
  data: {
    keyword:"",
    result:"",
    searchCities:[],
    visitedCities:[],
    location:""
  },
  onLoad(){
    /* (res=>{
    let result = res.data.hotCities
  }) */
  /* 定位的城市 */
  var location = wx.getStorageSync('locationCity');
  if(location){
    this.setData({
      location
    })
  }
    var cities = wx.getStorageSync('cities');
    if(cities){
      this.setData({
        visitedCities:cities
      })
    }else{
      wx.setStorageSync('cities', [])
    }
    http({url:":8000/city"}).then(res=>{
      let result = res.data.data;
      let lists = Object.keys(result.cities)
      this.setData({
        result,
        lists
      })
    })
  },
  handleClick(event){

    /* 判断缓存 */
    var cities = wx.getStorageSync('cities'); 
    var name = event.currentTarget.dataset.name;
    if(!cities.includes(name)){
      cities.unshift(name);
    }
    wx.setStorageSync('cities', cities.slice(0,3));
    app.globalData.currentCity = name;
    // console.log(name);
    wx.navigateBack();
  },
  handlechange(event){
    var keyword = event.detail
    var searchCities = this.filterCity(keyword);
    this.setData({
      keyword,
      searchCities
    })
  },
  filterCity(keyword){
    var cities = this.data.result.cities;
    var arr = [];
    for(let key in cities){
      cities[key].forEach(item=>{
        if(item.name.includes(keyword) || item.spell.includes(keyword)){
          arr.push(item);
        }
      })
    }
    return arr;
  }
/*   onLoad: function (options) {
    console.log("onLoad");
    app.globalData.currentCity = this.data.city
    getHttpCat({
      success:res=>{
        console.log(res);
        this.setData({
          city:res.data.data.cities
        })
      }
    })
  }, *//* 
  onShow() {
    console.log("show");
  },
  onHide() {
    console.log("onHide");
  },
  onReady() {
    console.log("ready");
  },
  onUnload() {
    console.log("onUnload");
  }, */
})